#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import django

# 添加项目路径到系统路径
backend_path = os.path.join(os.path.dirname(__file__), '..', 'backend')
sys.path.append(backend_path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'massage_app.settings')

# 初始化Django
django.setup()

import io
from backend.tools.minio_client import minio_client
from backend.common.image_crawler import image_crawler
from services.models import Banner, Category, Technician, Service

def generate_and_save_images():
    """生成并保存图片"""
    # 清空现有数据
    Banner.objects.all().delete()
    Category.objects.all().delete()
    Technician.objects.all().delete()
    Service.objects.all().delete()
    
    # 处理图标（使用SVG生成）
    icons = {
        "massage": ("按摩", "#888"),
        "spa": ("SPA", "#4a90e2"),
        "footcare": ("足疗", "#e24a6a"),
        "bodycare": ("身体护理", "#7ed321")
    }
    
    icon_urls = {}
    for name, (text, color) in icons.items():
        print(f"处理图标: {name}")
        url = image_crawler.process_icon(name, text, color)
        if url:
            icon_urls[name] = url
            print(f"图标上传成功: {name} -> {url}")
        else:
            print(f"图标处理失败: {name}")
    
    # 处理轮播图（使用SVG生成）
    banners = {
        "banner1": ("秋季养生特惠", "专业按摩服务，享受健康生活", "#4a90e2"),
        "banner2": ("新用户专享", "首次预约立减50元", "#e24a6a")
    }
    
    banner_urls = {}
    for name, (title, description, color) in banners.items():
        print(f"处理轮播图: {name}")
        url = image_crawler.process_banner(name, title, color)
        if url:
            banner_urls[name] = url
            print(f"轮播图上传成功: {name} -> {url}")
        else:
            print(f"轮播图处理失败: {name}")
    
    # 处理技师头像（使用SVG生成）
    technicians = {
        "technician1": ("张技师", "#ccc"),
        "technician2": ("李技师", "#bbb"),
        "technician3": ("王技师", "#aaa")
    }
    
    technician_urls = {}
    for name, (text, color) in technicians.items():
        print(f"处理技师头像: {name}")
        url = image_crawler.process_avatar(name, text, color)
        if url:
            technician_urls[name] = url
            print(f"技师头像上传成功: {name} -> {url}")
        else:
            print(f"技师头像处理失败: {name}")
    
    # 处理服务图片（使用SVG生成）
    services = {
        "service1": ("全身放松按摩", "#3cc51f"),
        "service2": ("足底按摩", "#4a90e2")
    }
    
    service_urls = {}
    for name, (text, color) in services.items():
        print(f"处理服务图片: {name}")
        url = image_crawler.process_service_image(name, text, color)
        if url:
            service_urls[name] = url
            print(f"服务图片上传成功: {name} -> {url}")
        else:
            print(f"服务图片处理失败: {name}")
    
    # 保存到数据库
    # 创建轮播图数据
    if "banner1" in banner_urls:
        Banner.objects.create(
            title="秋季养生特惠",
            description="专业按摩服务，享受健康生活",
            image=banner_urls["banner1"],
            sort_order=1
        )
    
    if "banner2" in banner_urls:
        Banner.objects.create(
            title="新用户专享",
            description="首次预约立减50元",
            image=banner_urls["banner2"],
            sort_order=2
        )
    
    # 创建服务分类数据
    if "massage" in icon_urls:
        Category.objects.create(
            name="massage",
            icon=icon_urls["massage"],
            sort_order=1
        )
    
    if "spa" in icon_urls:
        Category.objects.create(
            name="spa",
            icon=icon_urls["spa"],
            sort_order=2
        )
    
    if "footcare" in icon_urls:
        Category.objects.create(
            name="footcare",
            icon=icon_urls["footcare"],
            sort_order=3
        )
    
    if "bodycare" in icon_urls:
        Category.objects.create(
            name="bodycare",
            icon=icon_urls["bodycare"],
            sort_order=4
        )
    
    # 创建技师数据
    if "technician1" in technician_urls:
        Technician.objects.create(
            name="张技师",
            avatar=technician_urls["technician1"],
            rating=4.8,
            order_count=128
        )
    
    if "technician2" in technician_urls:
        Technician.objects.create(
            name="李技师",
            avatar=technician_urls["technician2"],
            rating=4.9,
            order_count=96
        )
    
    if "technician3" in technician_urls:
        Technician.objects.create(
            name="王技师",
            avatar=technician_urls["technician3"],
            rating=4.7,
            order_count=142
        )
    
    # 创建服务数据
    if "service1" in service_urls:
        Service.objects.create(
            name="全身放松按摩",
            category="massage",
            description="专业的全身放松按摩服务，缓解疲劳",
            duration=60,
            price=198.00,
            image=service_urls["service1"]
        )
    
    if "service2" in service_urls:
        Service.objects.create(
            name="足底按摩",
            category="footcare",
            description="专业的足底按摩服务，促进血液循环",
            duration=45,
            price=128.00,
            image=service_urls["service2"]
        )
    
    print("所有图片已生成、上传并保存到数据库")
    
    # 返回所有URL用于测试
    return {
        "icons": icon_urls,
        "banners": banner_urls,
        "technicians": technician_urls,
        "services": service_urls
    }

if __name__ == "__main__":
    urls = generate_and_save_images()
    print("\n生成的图片URL:")
    for category, url_dict in urls.items():
        print(f"\n{category}:")
        for name, url in url_dict.items():
            print(f"  {name}: {url}")